package com.syni.mdd.yhd.common.all.service;

import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsGroupMessageReportDao;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessGroupMessage;
import com.syni.mdd.yhd.common.all.entity.BmsGroupMessageReport;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Optional;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsGroupMessageReportService extends BaseService<BmsGroupMessageReport, Integer> {

    @Autowired
    BmsGroupMessageReportDao bmsGroupMessageReportDao;

    @Autowired
    BmsBusinessGroupMessageService bmsBusinessGroupMessageService;


    @Override
    public BmsGroupMessageReportDao getBaseDao() {
        return bmsGroupMessageReportDao;
    }

    public Map<String,Object> doGroupMessageReport(Integer userId,Integer groupMessageId,
                                                   Integer reportType,String reportContent){
        Optional<BmsBusinessGroupMessage> optionalBmsBusinessGroupMessage = bmsBusinessGroupMessageService.getBaseDao().findById(groupMessageId);
        if(!optionalBmsBusinessGroupMessage.isPresent()){
            return ResultMapHelper.result(ResultCode.CODE_BUSINESS_USER_GRUOP_MESSAGE_NO_EXIST, ResultMsg.CODE_BUSINESS_USER_GRUOP_MESSAGE_NO_EXIST);
        }
        BmsBusinessGroupMessage bmsBusinessGroupMessage = optionalBmsBusinessGroupMessage.get();
        BmsGroupMessageReport bmsGroupMessageReport = new BmsGroupMessageReport();
        bmsGroupMessageReport.setBmsUserId(userId);
        bmsGroupMessageReport.setGroupMessageId(groupMessageId);
        bmsGroupMessageReport.setReportType(reportType);
        bmsGroupMessageReport.setReportContent(reportContent);
        bmsGroupMessageReport.setNewTime(new Date());
        bmsGroupMessageReport.setContent(bmsBusinessGroupMessage.getContent());
        bmsGroupMessageReportDao.save(bmsGroupMessageReport);
        return ResultMapHelper.success(null,"举报成功");
    }

}
